package com.juc.list;

import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @Author HK
 * @Date 2022/5/12 22:36
 * @Version 1.0
 */

public class NotSafeDemo {

    public static void main(String[] args) {

        /**
         *
         * 报错：ConcurrentModificationException：并发修改异常
         * 导致原因：ArrayList是线程不安全的
         * 解决方案：
         *      1 Vector
         *      2 Collections.synchronizedList(new ArrayList<>())
         *      3 juc包下的CopyOnWriteArrayList
         */

        List<String> list = new CopyOnWriteArrayList<>();//Collections.synchronizedList(new ArrayList<>()); //new Vector<>(); //new ArrayList<>();

        for (int i = 0; i < 30; i++) {
            new Thread(() -> {
                list.add(UUID.randomUUID().toString().substring(0,8));
                System.out.println(list);
            }, String.valueOf(i)).start();
        }
    }


}
